
public class Evaluator 
{
	int[][] state;
	int[][] weights;
	
	public Evaluator(int [][] currentState)
	{
		state=currentState;
		
		//create weights for positions on the board
		weights=computeWeight(state.length,state[0].length);
	}
	
	//evaluation function works by summing the computed weights
	public int Eval()
	{
		int maxVal = Integer.MAX_VALUE;
		int minVal = Integer.MIN_VALUE;
		boolean haswon1=HasWon(1);
		boolean haswon2=HasWon(2);
		int score = 0;
		
		if(haswon1 && haswon2)
			return 0;
		if(haswon1)
			return maxVal;
		if(haswon2)
			return minVal;
		
		for(int column=0; column<state[0].length; column++)
		{
			for(int row=0; row<state.length; row++)
			{
				if(state[row][column]==1)
					score+=weights[row][column];
				else if(state[row][column]==2)
					score-=weights[row][column];
			}
			//System.out.println();
		}
		//System.out.println(score);
		return score;
	}
	private int[][] computeWeight(int rows, int columns)
	{
		int[][] retWeight = new int[rows][columns];
		int halfCol = columns/2;
		int halfRow = rows/2;
		int colscore = 0;
		int rowscore = 0;
		
		//find weights for each place on the board
		for(int column=0; column<retWeight[0].length; column++)
		{
			colscore = Math.abs((halfCol) - column);
			colscore = halfCol - colscore;
			for(int row=0; row<retWeight.length; row++)
			{
				rowscore = Math.abs((halfRow) - row);
				rowscore = halfRow - rowscore;
				retWeight[row][column] = rowscore + colscore;
			}
		}
		return retWeight;
		
	}
	boolean HasWon(int player)
    {
    	 boolean win = false;
     	
     	//horizontal and vertical check
     	for (int row=0; row<state[0].length; row++) {
     	  for (int column=0; column<(state.length-3); column++) {
     	    if (state[column][row] == player &&
     	        state[column][row] == state[column+1][row] &&
     	        state[column][row] == state[column+2][row] &&
     	        state[column][row] == state[column+3][row]) {
     	      win = true;
     	    }
     	  }
     	}
     	for (int row=0; row<(state[0].length-3); row++) {
     	  for (int column=0; column<state.length; column++) {
     	    if (state[column][row] == player &&
     	        state[column][row] == state[column][row+1] &&
     	        state[column][row] == state[column][row+2] &&
     	        state[column][row] == state[column][row+3]) {
     	      win = true;
     	    }
     	  }
     	}
     	
     	// diagonals check
     	for (int row=0; row<(state[0].length-3); row++) {
     	  for (int column=0; column<(state.length-3); column++) {
     	    if (state[column][row] == player &&
     	        state[column][row] == state[column+1][row+1] &&
     	        state[column][row] == state[column+2][row+2] &&
     	        state[column][row] == state[column+3][row+3]) {
     	      win = true;
     	    }
     	  }
     	}
     	
     	for (int row=3; row<state[0].length; row++) {
     	  for (int column=0; column<(state.length-3); column++) {
     	    if (state[column][row] == player &&
     	        state[column][row] == state[column+1][row-1] &&
     	        state[column][row] == state[column+2][row-2] &&
     	        state[column][row] == state[column+3][row-3]) {
     	      win = true;
     	    }
     	  }
     	}
     	
     	return win;
    }
		
	
	

}
